﻿using System.Security.Policy;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums;
using ZBJF.Warranty.H5.Controllers;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.WebProvider;
using WebContext = ZBJF.Warranty.WebProvider.WebContext;

namespace ZBJF.Warranty.H5
{
    public class WeiXinLoginFilterAttribute : ActionFilterAttribute
    {
        //表示是否检查登录
        public bool CheckLogin { get; set; }
        //Action方法执行之前执行此方法
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            var isOpenInWeiXin = CommonMethod.IsRequestFormWeiXin(filterContext.HttpContext.Request.UserAgent);
            object openId = HttpContext.Current.Session["OpenId"];

            if (CheckLogin && isOpenInWeiXin)
            {
                //校验用户是否已经登录
                if (openId == null)
                {
                    filterContext.HttpContext.Response.Redirect("/WeiXin/WxLogin?backUrl=" +HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString()));
                }
            }
        }
    }
}
